<?php
Prado::using('System.I18N.core.NumberFormat');
Prado::using('Application.common.common');
class ProductDetails extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Product Details";
		
		if (!$this->IsPostBack)
		{
			$activeRecord = $this->getItem();
			if ($activeRecord instanceof ProductRecord)
			{
			}
		}

	}
	
	public function getItem()
	{
		if ($this->Request->Contains("id") && $this->Request->Contains("alias"))
		{
			// use Active Record to look for the specified post ID
			$activeRecord = ProductRecord::finder()->findByproduct_idAndproduct_alias(TPropertyValue::ensureInteger($this->Request['id']), $this->Request['alias']);
			if($activeRecord === null)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","product");
				$this->mainContent->Visible = false;
			}
			return $activeRecord;
		}
		else
		{
			$this->Notice->Type = UserNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","product");
			$this->mainContent->Visible = false;
		}
	}
	
	protected function btnAddToCart_Clicked($sender, $param)
	{
		// do add cart here
		try
		{
			$cartTemp = CartTempRecord::finder()->findByPk($this->Session->SessionID);
			if (!($cartTemp instanceof CartTempRecord))
			{
				$cartTemp = new CartTempRecord;
				$cartTemp->SessionID = $this->Session->SessionID;
				if (!$this->User->IsGuest) $cartTemp->UserID = $this->User->ID;
				$cartTemp->save();
			}
			// we should check if user select the same product
			$cartDetail = CartTempDetailRecord::finder()->findBysession_idAndproduct_id($this->Session->SessionID,$this->Item->ID);				
			if ($cartDetail instanceof CartTempDetailRecord)
			{
				$cartDetail->Quantity = $cartDetail->Quantity + 1;
			}
			else
			{
				$cartDetail = new CartTempDetailRecord;
				$cartDetail->HashID = md5(uniqid(time()));
				$cartDetail->SessionID = $cartTemp->SessionID;
				if (!$this->User->IsGuest) $cartDetail->UserID = $this->User->ID;
				$cartDetail->ProductID = $this->Item->ID;
				$cartDetail->Quantity = 1;
				
				$productRecord = ProductRecord::finder()->findByPk($this->Item->ID);
				if ($productRecord instanceof ProductRecord)
				{
					$cartDetail->ProductPrice = $productRecord->Price;
				}
				$cartDetail->CreateDate = time();
			}
			$cartDetail->Subtotal = $cartDetail->Quantity*Common::roundTo($cartDetail->ProductPrice);
			
			$cartDetail->save();
			$this->Response->redirect($this->Service->ConstructUrl("shop.ShoppingCart"));
			//$this->ajaxCart->refreshCart();
		}
		catch(TException $e)
		{
			echo $e;
			//$this->Notice->Type = UserNoticeType::Error;
			//$this->Notice->Text = $this->Application->getModule("message")->translate("UNKNOWN_ERROR");			
		}
	}
}

?>